<?php
defined('IN_WEB') or die('Include Error!');

class ModelMember extends otable {
	
	/**
	 * 根据用户ID获取资料
	 * 
	 */
	public function getOneById( $mid ){
		if( ! $mid = functions::uint( $mid)){
			return array();
		}
		$sql = "SELECT * FROM $this->tblmb a LEFT JOIN $this->tblmf b ON a.m_id=b.m_id WHERE a.m_id='$mid' LIMIT 1";
		$aInfo = odb::db()->fetchArray( $sql, MYSQL_ASSOC);
		return $aInfo['m_id']==$mid ? $aInfo : array();
	}
	
	/**
	 * 根据sitemid和sid获取资料,sid默认是新浪微博
	 */
	public function getOneBySitemid( $sitemid, $sid=1){
		$sitemid = odb::db()->escape( $sitemid);
		if( (! strlen( $sitemid)) || (! $sid = functions::uint( $sid))){
			return array();
		}

		$mid = model::member()->getMidBySitemid( $sitemid, $sid );
		return model::member()->getOneById( $mid );
	}
	
	/**
	 * 根据sitemid与sid来获得用户的mid
	 *
	 * @param int $sitemid
	 * @param int $sid
	 * @return int $mid
	 */
	public function getMidBySitemid( $sitemid, $sid ) {
		$sitemid = odb::db()->escape( $sitemid);
		if( (! strlen( $sitemid)) || (! $sid = functions::uint( $sid))){
			return 0;
		}
		
		$query = "SELECT m_id FROM $this->tblmb WHERE m_sitemid='$sitemid' and s_id ='$sid' LIMIT 1";
		$aMid = odb::db()->fetchArray( $query, MYSQL_ASSOC );
		$mid = (int)$aMid['m_id'];
		return $mid;
	}
	
	
	/**
	 * 插入一个用户. 插入主表获得mid,插入附表
	 * @param Array $info 对应主/副表的字段
	 * @return int/Array 失败为0否则返回信息数组
	 */
	public function insert( $aSet ){
		$sitemid = $aSet['m_sitemid'] = odb::db()->escape( $aSet['m_sitemid']);
		$sid = $aSet['s_id'] = functions::uint( $aSet['s_id']);
		$mname= $aSet['m_name'] = odb::db()->escape( $aSet['m_name']);
		$mnick = $aSet['m_nick'] = odb::db()->escape( $aSet['m_nick']);
		$msex = $aSet['m_sex'] = functions::uint( $aSet['m_sex']);
		$micon = $aSet['m_micon'] = odb::db()->escape( $aSet['m_micon']);
		$mlocation = $aSet['m_location'] = odb::db()->escape( $aSet['m_location']);
		
		$mblogurl = $aSet['mf_blogurl'] = odb::db()->escape( $aSet['mf_blogurl']);
		$mhomepage = $aSet['mf_homepage'] = odb::db()->escape( $aSet['mf_homepage']);
		$mfanscount = $aSet['mf_fanscount'] = functions::uint( $aSet['mf_fanscount']);
		$mfriendcount = $aSet['mf_friendcount'] = functions::uint( $aSet['mf_friendcount']);
		$mweibocount = $aSet['mf_weibocount'] = functions::uint( $aSet['mf_weibocount']);
		$mdesc = $aSet['mf_desc'] = odb::db()->escape( $aSet['mf_desc']);
		$mtoken = $aSet['mf_token'] = odb::db()->escape( $aSet['mf_token']);
		$msecret = $aSet['mf_secret'] = odb::db()->escape( $aSet['mf_secret']);
		
		$mstatus = $aSet['m_status'] = 1;
		$mactivetime = $aSet['m_activetime'] = time();
		$mentercount = $aSet['m_entercount'] = 1;  
		$mversion = $aSet['m_version'] = 1;
		
		if(( ! strlen( $sitemid)) || (! $sid)){
			return 0;
		}
		
		$query = "INSERT INTO $this->tblmb SET m_sitemid='$sitemid',s_id='$sid',m_name='$mname',m_nick='$mnick'
					,m_sex='$msex',m_micon='$micon',m_location='$mlocation',m_status='$mstatus'
					,m_activetime='$mactivetime',m_entercount='$mentercount',m_version='$mversion' 
					ON DUPLICATE KEY UPDATE m_entercount=m_entercount+1";
		odb::db()->query( $query);
		$mid = $aSet['mid'] = odb::db()->insertID();
		
		$query = "INSERT DELAYED IGNORE INTO $this->tblmf SET m_id='$mid',mf_blogurl='$mblogurl'
					,mf_homepage='$mhomepage',mf_fanscount='$mfanscount',mf_friendcount='$mfriendcount'
					,mf_weibocount='$mweibocount',mf_desc='$mdesc',mf_token='$mtoken',mf_secret='$msecret'";
		odb::db()->query( $query);

		return $mid > 0 ? $aSet : 0;
	}
}